﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using KSS.Generator.Configuration;
using KSS.Generator.Schema;

namespace KSS.Generator
{
    public class LinqMapperProjectGenerator : ProjectGenerator
    {
        public override string ProjectName
        {
            get { return Template.Project.LinqMapperProjectName; }
        }

        protected override void GenerateFile(ProjectSetting projectConfig, FileInfo templateFile, DirectoryInfo generatedDirectory)
        {
            switch (templateFile.Name)
            {
                case Template.LinqMapperProject.GeneratedMapperFileName:
                    GenerateMapperFile(projectConfig, templateFile, generatedDirectory);
                    break;

                case Template.LinqMapperProject.CustomMapperFileName:
                    GenerateCustomMapperFile(projectConfig, templateFile, generatedDirectory);
                    break;
            }
        }

        private void GenerateMapperFile(ProjectSetting projectConfig, FileInfo templateFile, DirectoryInfo generatedDirectory)
        {
            // Get entity mapper template file content
            string templateContent = GeneratorHelper.ReadFromFile(templateFile.FullName);

            // Mapper file name template
            string fileNameFormat = GeneratorHelper.GetFileNameFromat(templateFile.Name);

            // Fill methods to template
            foreach (Table t in Context.DatabaseSchema.Tables)
            {
                #region { Entity name custom tag }

                // fill the entity class name
                projectConfig.SetPropertyValue(Template.LinqMapperProject.CustomTag_EntityName, t.DataType.Name);
                #endregion

                #region { Entity mapping custom tag}

                StringBuilder sbEntityMapping = new StringBuilder();

                // generate assignment command
                foreach (Column c in t.DataType.Columns)
                {
                    sbEntityMapping.Append(string.Format(Template.LinqMapperProject.Mapping, c.Name));
                }

                projectConfig.SetPropertyValue(Template.LinqMapperProject.CustomTag_EntityMapping, sbEntityMapping.ToString());
                #endregion

                // Fill property in template
                string sourceCode = GeneratorHelper.FillProperties(projectConfig, templateContent);

                // Write source code to file
                string generatedFilePath = string.Format("{0}\\{1}",
                            generatedDirectory.FullName,
                            string.Format(fileNameFormat, t.DataType.Name));

                GeneratorHelper.WriteToFile(generatedFilePath, sourceCode);
            }
        }

        private void GenerateCustomMapperFile(ProjectSetting projectConfig, FileInfo templateFile, DirectoryInfo generatedDirectory)
        {
            // Get entity mapper template file content            
            string templateContent = GeneratorHelper.ReadFromFile(templateFile.FullName);

            // Mapper file name template            
            string fileNameFormat = GeneratorHelper.GetFileNameFromat(templateFile.Name);


            // Fill methods to template
            foreach (Table t in Context.DatabaseSchema.Tables)
            {
                #region { Entity name custom tag }

                // fill the entity class name
                projectConfig.SetPropertyValue(Template.LinqMapperProject.CustomTag_EntityName, t.DataType.Name);
                #endregion

                #region { Entity mapping custom tag}

                StringBuilder sbEntityMapping = new StringBuilder();

                // generate assignment command
                foreach (Column c in t.DataType.Columns)
                {
                    sbEntityMapping.Append(string.Format(Template.LinqMapperProject.Mapping, c.Name));
                }

                projectConfig.SetPropertyValue(Template.LinqMapperProject.CustomTag_EntityMapping, sbEntityMapping.ToString());
                #endregion

                // Fill property in template                
                string sourceCode = GeneratorHelper.FillProperties(projectConfig, templateContent);

                // Write source code to file
                string generatedFilePath = string.Format("{0}\\{1}",
                            generatedDirectory.FullName,
                            string.Format(fileNameFormat, t.DataType.Name));

                GeneratorHelper.WriteToFile(generatedFilePath, sourceCode);
            }
        }
    }
}
